package mmc.networking;

import java.io.IOException;
import java.util.ArrayList;

import mmc.RemoteServer;
import mmc.networking.data.Update;

@SuppressWarnings("serial")
public class UpdateMessage extends Message{
	public Update[] updates;
	
	private transient ArrayList<Update> updateList = new ArrayList<Update>();
	
	public void addUpdate(Update u){
		updateList.add(u);
	}
	
	public void pack(){
		updates = updateList.toArray(new Update[updateList.size()]);
		updateList = null;
	}
	
	@Override
	public void executeOnClient(RemoteServer context) {
		context.getClient().getScene().addUpdate(this);
	}
	
	private void writeObject(java.io.ObjectOutputStream out) throws IOException{
		out.writeInt(updates.length);
		for(Update u : updates)
			out.writeUnshared(u);
	}
	
	private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException{
		updates = new Update[in.readInt()];
		
		for(int i=0;i<updates.length;i++)
			updates[i] = (Update) in.readObject();
	}
}
